package com.zyf.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zyf.pojo.MedicalRecord;
import com.zyf.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class MedicalRedcordController {

    //注入service
    @Autowired
    private MedicalRecordService medicalRecordService;

    /*处理保存病历的请求*/
    @PostMapping("/saveMedicalRecord")
    public Map<String,Object> saveMedicalRecord(@RequestBody MedicalRecord medicalRecord){
        Map<String,Object> map=new HashMap<>();
        System.out.println("==="+medicalRecord);
        try{
            medicalRecordService.save(medicalRecord);
            map.put("msg","病历保存成功....");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","病历保存失败.....");
            map.put("code",500);
        }
        return map;
    }

    /*处理保持确诊结果的请求*/
    @RequestMapping("/saveCheckedResult")
    public Map<String,Object> saveCheckedResult(
            @RequestBody MedicalRecord medicalRecord){
        /*接受前端传送的form表单更新medicalRecord的同时，将register表中的看诊状态改为3，
        这两个表根据register的id和medicalRecord的register_id进行连接*/


        Map<String,Object> map=new HashMap<>();
        System.out.println("==="+medicalRecord);
        try{
            UpdateWrapper<MedicalRecord> wrapper=new UpdateWrapper<>();
            wrapper.eq("register_id",medicalRecord.getRegisterId());
            medicalRecordService.update(medicalRecord,wrapper);

            map.put("msg","确诊保存成功....");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","确诊保存失败.....");
            map.put("code",500);
        }
        return map;
    }
}
